package com.muqi.app.user.widget;

import java.io.InputStream;
import java.util.Random;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.muqi.app.qlearn.student.R;

public class TreeView extends LinearLayout {
	private Paint mPaint;

	private int progress;

	Random random = new Random();

	private Bitmap bitmapbg;

	private Bitmap bitmap;

	private Context mContext;

	public TreeView(Context context) {
		super(context);
		init(context);
	}

	public TreeView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public TreeView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public void setProgress(int progress) {
		this.progress = progress;

	}

	@Override
	protected void dispatchDraw(Canvas canvas) {
		super.dispatchDraw(canvas);
		// Rect rect = new Rect();
		// this.mPaint.getTextBounds("hello", 0, "hello".length(), rect);
		// this.mPaint.setTextSize(18f);
		// int x = (getWidth() / 2) - rect.centerX();// 让现实的字体处于中心位置;
		// int y = (getHeight() / 2) - rect.centerY();// 让显示的字体处于中心位置;
		// canvas.drawText("hello", x, y, this.mPaint);

		canvas.drawBitmap(bitmapbg, 0, 0, this.mPaint);

		int x1 = 20;
		int y1 = 0;
		for (int i = 0; i < progress; i++) {
			Bitmap bitmap = null;
			AssetManager manager = mContext.getAssets();
			try {
				InputStream in = manager.open("treepic/icon_homework_follower.png");
				bitmap = BitmapFactory.decodeStream(in);
			} catch (Exception e) {
				
			}
			
			if (i % 20 == 0) {
				x1 = x1 + 40;
				y1 = 0;
			}
			y1 = y1 + 40;
			
			canvas.drawBitmap(bitmap, x1, y1, this.mPaint);
			bitmap.recycle();
			
			
		}

		invalidate();
	}

	// 初始化，画笔
	private void init(Context context) {
		mContext = context;
//		setOrientation()
		// LayoutInflater inflater = LayoutInflater.from(context);
		// View view = inflater.inflate(R.layout.tree_layout, null);
		InputStream isbg = getResources().openRawResource(
				R.drawable.mine_title_bg);
		bitmapbg = BitmapFactory.decodeStream(isbg);

//		InputStream is = getResources().openRawResource(R.drawable.icon_homework_follower);
//		bitmap = BitmapFactory.decodeStream(is);

		this.mPaint = new Paint();
		this.mPaint.setAntiAlias(true);// 设置抗锯齿;
//		this.mPaint.setColor(Color.parseColor("#0ec16b"));

	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {

	}
}
